// @FileName  :h7t3.c
// @Time      :2024/5/24 14:13
// @Author    :YKW
//# include <bits/stdc++.h>
//using namespace std;
# include <stdio.h>
# include <stdlib.h>
# include <math.h>
# include <string.h>
# include <stdlib.h>

#define N 1009
struct edge {
    int id, u, v, wgt;
} G[N]; //邻接表
//点 边 。 。 。 每个点的度 1 当前记录的路线 是否经过某边 是否经过某点
int n, m, cnt, sum, deg[N], ans[N], visEdge[N], visPoint[N], f[N];

int cmp_w(const void *a, const void *b) {
    return ((struct edge *) a)->wgt - ((struct edge *) b)->wgt;
}

int cmp_id(const void *a, const void *b) {
    return *(int *) a - *(int *) b;
}

int find(int x) {
    return f[x] == x ? x : find(f[x]);
}

void uni(int x, int y) {
    f[find(x)] = find(y);
}

void kruskal() {
    qsort(G, m, sizeof(struct edge), cmp_w);
    for (int i = 0; i < m; i++) {
        if (find(G[i].u) != find(G[i].v)) {
            ans[cnt++] = G[i].id;
            sum += G[i].wgt;
            uni(G[i].u, G[i].v);
        }
    }
    qsort(ans, cnt, sizeof(ans[0]), cmp_id);
    printf("%d\n", sum);
    for (int i = 0; i < cnt; i++)
        printf("%d ", ans[i]);
}

int main() {
    scanf("%d%d", &n, &m);
    for (int i = 0; i < m; i++)
        scanf("%d%d%d%d", &G[i].id, &G[i].u, &G[i].v, &G[i].wgt);
    for (int i = 0; i < n; i++)
        f[i] = i;
    kruskal();
    return 0;
}
/*
6 10
1 0 1 600
2 0 2 100
3 0 3 500
4 1 2 500
5 2 3 500
6 1 4 300
7 2 4 600
8 2 5 400
9 3 5 200
10 4 5 600
 */